package com.nowcoder.community.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Component
public class MailClient {
    private static Logger logger= LoggerFactory.getLogger(MailClient.class);

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Value("${spring.mail.username}")
    private String from;
    public void sendMail(String to,String subject,String content){
        MimeMessage message=javaMailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(message);


        try {
            helper.setTo(to);
            helper.setFrom(from);
            helper.setText(content,true);
            helper.setSubject(subject);

            javaMailSender.send(message);

        } catch (MessagingException e) {
           logger.error("发送邮件失败" + e.getMessage());
        }


    }
}
